package util;

import ids.ProbeSensor;
import ids.Sensor;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Vector;

import opt.FitnessFunction;

import ns2.Field;
import ns2.Topology;

public class StatSensorLocation {

	public Topology topology;
	public Vector<Sensor> sensors;
	public float fitness;
	public int[] bestSoFar;

	public FitnessFunction myFitness;

	public StatSensorLocation(FileReader fileReader, int[] bestSoFar) {
		this.topology = fileReader.getTopology();
		this.sensors = new Vector<Sensor>();
		for (int i = 0; i < bestSoFar.length; i++) {
			this.sensors.addElement(new ProbeSensor());
			this.sensors.elementAt(i).setEfficiency((float) 6.72);
			this.sensors.elementAt(i).setDeploymentCost(8);
			this.sensors.elementAt(i).setMonitoringCost(3);
		}
		this.bestSoFar = bestSoFar;
		this.fitness = 0;
		this.myFitness = new FitnessFunction();
	}

	public String print() {
		StringBuffer result = new StringBuffer();
		Connections connections = new Connections();
		int totalAlarms = 0;
		for (int i = 0; i < this.sensors.size(); i++) {
			this.sensors.elementAt(i).setLocation(
					this.topology.nodes.getNode(this.bestSoFar[i]));
			this.sensors.elementAt(i).run();
			connections.addAll(sensors.elementAt(i).location.connections);
		}

		Vector<Connections> groupBySources = connections.sort(Field.Src_addr);

		for (int i = 0; i < groupBySources.size(); i++) {
			if (groupBySources.elementAt(i).size() >= ProbeSensor.lower_threshold) {
				totalAlarms += groupBySources.elementAt(i).size();
				for (int j = 0; j < groupBySources.elementAt(i).size(); j++) {
					result.append(groupBySources.elementAt(i).elementAt(j)
							.getSrc()
							+ " attacks "
							+ groupBySources.elementAt(i).elementAt(j).getDst()
							+ "\n");
				}
			}
		}
		// result.append("Total number of alarms: " + totalAlarms + "\n");
		result.append(myFitness.evaluateAndPrint(this.sensors));
		return result.toString();
	}

	public static void main(String[] args) {
		FileReader fileReader = null;
		String source = new String();
		try {
			fileReader = new FileReader(new File(args[0]), new File(args[1]),
					new File(args[2]));
			fileReader.readTraceFile();
			fileReader.readTopologyFile();
			fileReader.process();
		} catch (SSIDException e) {
			System.err.println(e.getMessage());
			System.err.print(e.getExSearchUsage());
			System.exit(1);
		}
		boolean quit = false;
		while (!quit) {
			System.out
					.println("Please input sensors location: (input quit to leave))");
			BufferedReader in = new BufferedReader(new InputStreamReader(
					System.in));
			try {
				source = in.readLine();
			} catch (IOException e) {
				System.err.println("Read error!");
			}
			StringTokenizer st = new StringTokenizer(source);
			int[] bestSoFar = new int[st.countTokens()];
			int i = 0;
			while (st.hasMoreTokens()) {
				String next = st.nextToken();
				if (next.equalsIgnoreCase("quit")) {
					quit = true;
				} else {
					bestSoFar[i++] = Integer.parseInt(next);
				}
			}
			if (!quit) {
				StatSensorLocation ssl = new StatSensorLocation(fileReader,
						bestSoFar);
				System.out.print(ssl.print());
			}
		}
	}
}
